import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout/index.vue'

// 静态路由
export const staticRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: { name: 'home' },
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/index'),
        name: 'home',
        meta: { title: '首页', icon: 'home' }
      },
      {
        path: '/article/create',
        component: () => import('@/views/articleManger/create'),
        name: 'article_create',
        meta: { title: '新增内容' },
        hidden: true
      },
      {
        path: '/article/edit',
        component: () => import('@/views/articleManger/edit'),
        name: 'article_edit',
        meta: { title: '编辑内容' },
        hidden: true
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  }
]

/*
* 带权限控制的动态路由
* 非根节点只有一个子项需要展示分级可设置 alwaysShow: true
* 多个子节点一定要指定父级的meta
* path和子path不要重复
*/
export const dynamicRoutes = [
  {
    path: '/account',
    component: Layout,
    children: [
      {
        path: '/account',
        component: () => import('@/views/account/index'),
        name: 'account',
        meta: { title: '账号管理', icon: 'account', auth: 'account' }
      }
    ]
  },
  {
    path: '/role',
    component: Layout,
    children: [
      {
        path: '/role',
        component: () => import('@/views/role/index'),
        name: 'role',
        meta: { title: '角色管理', icon: 'role', auth: 'role' }
      }
    ]
  },
  {
    path: '/menu',
    component: Layout,
    children: [
      {
        path: '/menu/index',
        component: () => import('@/views/menu/index'),
        name: 'menu',
        meta: { title: '菜单管理', icon: 'menu', auth: 'menu' }
      }
    ]
  },
  {
    path: '/article',
    component: Layout,
    children: [
      {
        path: '/article/index',
        component: () => import('@/views/articleManger/index'),
        name: 'article',
        meta: { title: '文章管理', icon: 'article', auth: 'article' },
      }
    ]
  },
  {
    path: '/analyse',
    component: Layout,
    children: [
      {
        path: '/analyse/index',
        component: () => import('@/views/articleManger/analyse'),
        name: 'analyse',
        meta: { title: '统计分析', icon: 'analyse', auth: 'analyse' },
      }
    ]
  }
]

// 递归动态路由判断菜单权限
// item是路由
// menu是请求返回的权限组
export const filterRoute = (item,menu) => {
  return item.filter(v => {
    if(v.children){
      let arr = v.children.filter(va=>menu.indexOf(va.meta.auth) > -1)
      if(arr.length > 0){
        v = { ...v,children: filterRoute(arr,menu) }
        return true
      }else{
        return false
      }
    }else{
      if(v.meta && menu.indexOf(v.meta.auth) > -1){
        return true
      }else{
        return false
      }
    }
  })
}

// 创建路由
const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: staticRoutes
})

// 创建路由
const router = createRouter()

// 路由拦截
router.beforeEach((to, from, next) => {
  // 判断token
  if (sessionStorage.getItem('token')) {
    // 防止重复登录
    if (to.path === '/login') {
      Vue.prototype.$message.error('请不要重复登录')
      return next({ name: from.name ? from.name : 'home' })
    }
    next()
  } else {
    // 跳过登录页验证
    if (to.path === '/login') {
      return next()
    }
    Vue.prototype.$message.error('请先登录')
    next({ path: '/login' })
  }
})

export default router